package com.jdd.modules.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.CacheConstant;
import com.jdd.modules.parking.entity.ConfigParkCharge;
import com.jdd.modules.parking.mapper.ConfigParkChargeMapper;
import com.jdd.modules.parking.service.IConfigParkChargeService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 停车场计费规则信息表
 * @Author: jdd
 * @Date: 2021-02-18
 * @Version: V1.0
 */
@Service
public class ConfigParkChargeServiceImpl extends ServiceImpl<ConfigParkChargeMapper, ConfigParkCharge> implements IConfigParkChargeService {
    @Resource
    ConfigParkChargeMapper configParkChargeMapper;

    /**
     * 根据车辆类型ID获取对应的计费规则
     *
     * @param carTypeId 车辆类型id
     * @return 计费规则
     * @Description: 根据车辆类型ID获取对应的计费规则
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, key = "#carTypeId", unless = "#result==null")
    public ConfigParkCharge findConfigParkChargeByCarTypeId(String carTypeId) {
        LambdaQueryWrapper<ConfigParkCharge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigParkCharge::getCarTypeId, carTypeId);
        return configParkChargeMapper.selectOne(queryWrapper);
    }

    /**
     * 根据计费规则id获取对应的计费规则
     *
     * @param id 计费规则id
     * @return 计费规则
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_CHARGE_BY_ID, key = "#id", unless = "#result==null")
    public ConfigParkCharge findConfigParkChargeById(String id) {
        return this.getById(id);
    }

    /**
     * 删除对应车场编号的车场下的所有的计费规则信息
     *
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)
    })
    public Boolean removeByParkCode() {
        LambdaQueryWrapper<ConfigParkCharge> queryWrapper = new LambdaQueryWrapper<>();
        if (configParkChargeMapper.delete(queryWrapper) == 0) {
            return false;
        }
        return true;
    }

    /**
     * 保存计费规则实体
     *
     * @param configParkCharge 计费规则
     * @return 保存状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)})
    public boolean saveEntity(ConfigParkCharge configParkCharge) {
        if (configParkChargeMapper.insert(configParkCharge) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新计费规则实体
     *
     * @param configParkCharge 计费规则
     * @return 更新状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)})
    public boolean updateEntity(ConfigParkCharge configParkCharge) {
        if (configParkChargeMapper.updateById(configParkCharge) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 删除计费规则实体
     *
     * @param id 计费规则id
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)
    })
    public boolean deleteEntityById(String id) {
        if (configParkChargeMapper.deleteById(id) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 更新计费规则实体
     *
     * @param ids 计费规则id列表
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)})
    public boolean deleteEntityByIds(List<String> ids) {
        if (configParkChargeMapper.deleteBatchIds(ids) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 清空表数据
     *
     * @return true 清空成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_CAR_TYPE_ID, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_CHARGE_BY_ID, allEntries = true)})
    public boolean removeAllInfo() {
        try {
            configParkChargeMapper.removeAllInfo();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
